const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const path = require('path')
const request = require('request')
const log4 = require('./tools/logs')
const config = require('./config/production')
const wxPayModel = require('./models/wxPay')
const mdbs = require('./models/mongodbs')
const router = require('./router/router')
require('body-parser-xml')(bodyParser)
app.use(bodyParser())
app.use(bodyParser.xml({
    limit: '2MB', // Reject payload bigger than 1 MB
    xmlParseOptions: {
    normalize: true, // Trim whitespace inside text nodes
    normalizeTags: true, // Transform tags to lowercase
    explicitArray: false // Only put nodes in array if >1
    }
}))
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With")
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS")
    res.header("X-Powered-By",' 3.2.1')
    if(req.method=="OPTIONS") res.send(200)
    else  next()
})
app.get('/',function(req,res){
    res.redirect('app/index.html')
})
app.use(express.static(path.join(__dirname,'./static')))
app.post('/astokenOpid',router.astokenOpid)
app.get('/getPayurl',router.getPayurl)
app.post('/wxpaysign_api',wxPayModel.paySinsapi)
app.post('/wxNoctice',router.wxPayNotice)
app.get('/searchOrder',wxPayModel.searchOrder)
app.post('/loginAdmin',router.loginFunc)
app.get('/downloadbill',wxPayModel.downloadbill)
app.get('/wxpaylists',router.getWxpayList)
app.get('/getAllpage',router.getPageNum)
app.get('/getAllpageUser',router.getPageNumUser)
app.post('/getUserNum',router.getUserNum)
app.post('/getPayParameter',router.getPayParameter)
app.get('/getUserPayList',router.getUserPayList)
app.post('/void/v1/ismember',router.ismember)
app.post('/void/v1/isVipFunc',router.isVipFunc)
app.post('/void/v1/failmsg',router.failmsg)
app.post('/wxPayOrder',function(reqsss,ressss){
    let reqData =''
    try{
        reqData = reqsss.body}catch(err){
        log4.logs('下单接口接收参数报错')
        log4.logs(err)
    }
    if(reqData == ''){
        ressss.json({
            code:201,
            msg:'参数缺失'
        })
        return
    }
    var spbill_create_ip = reqsss.headers['x-real-ip'] ? reqsss.headers['x-real-ip'] : reqsss.ip.replace(/::ffff:/, '')
    var prepay_id = wxPayModel.order(reqData.attach, reqData.body, reqData.mch_id, reqData.openid, reqData.total_fee, reqData.notify_url,spbill_create_ip,reqsss,ressss) 
})
app.listen(config.port)
log4.logs('程序运行在'+config.port)












